<template>
    <div class="contract-detial-page contract-close-detial" :class="readOnly ? 'read':'write'">
        <Card>
            <div>
                <Affix :offset-top="120">
                    <div class="contract-detial-btns" v-if="isApprove">
                        <Button type="primary" @click="back">返回</Button>
                        <Button type="primary" v-if="!readOnly" @click="approve('pass')">审核通过</Button>
                        <Button type="primary" v-if="!readOnly" @click="approve('return')">退回</Button>
                    </div>
                    <div class="contract-detial-btns" v-else>
                        <Button type="primary" @click="back">返回</Button>
                        <Button type="primary" v-if="!readOnly" @click="save">保存</Button>
                        <Button type="primary" v-if="!readOnly" @click="submit">提交</Button>
                    </div>
                </Affix>
            </div>

            <Collapse v-model="openPanel">
                <Panel name="1" v-if="isApprove||this.processStatus==='approved'">
                    日志
                    <div class="content" slot="content">
                        <approveHistory :Obj="Obj"/>
                    </div>
                </Panel>
                <Panel name="2">
                    合同信息
                    <div class="content" slot="content">
                        <contractInfo ref="contractInfo" :Obj="Obj"/>
                    </div>
                </Panel>
                <Panel v-if="type=='OVERPAYMENT'" name="3">
                    溢缴款
                    <div class="content" slot="content">
                        <contractOverpayment
                            ref="business"
                            :Obj="Obj"
                            :pageParams="pageInfo"
                            :dataDict="dataDict"
                            :isApprove="isApprove"
                            @saveDataFn="saveDataFn"
                            @submitDataFn="submitDataFn"
                            @childBusinessForm="childBusinessForm"
                            @closeSpin="closeSpin"
                            @showModal="showModal"
                        />
                    </div>
                </Panel>
                <Panel v-if="type=='DIFFERENCE'" name="4">
                    差额
                    <div class="content" slot="content">
                        <contractDifference
                            :Obj="Obj"
                            :pageParams="pageInfo"
                            :dataDict="dataDict"
                            :isApprove="isApprove"
                            @saveDataFn="saveDataFn"
                            @submitDataFn="submitDataFn"
                            @childBusinessForm="childBusinessForm"
                            @closeSpin="closeSpin"
                            @showModal="showModal"
                        />
                    </div>
                </Panel>
                <Panel name="5" v-if="(!readOnly&&isApprove)||this.processStatus==='approved'">
                    当前用户: {{userInfo ? userInfo.userRealName :''}}
                    <div class="content" slot="content">
                        <contractCurrentUser
                            ref="contractCurrentUser"
                            :Obj="Obj"
                            @childUserForm="childUserForm"
                            @closeSpin="closeSpin"
                        />
                    </div>
                </Panel>
            </Collapse>
        </Card>
        <Modal
            :title="modalTitle"
            v-model="modal"
            :mask-closable="false"
            :width="500"
            :styles="{top: '90px'}"
        >
            <div v-if="modalType === 'differenceModal'">
                <expenseInfo :dataDict="dataDict" :contractNo="contractNo"/>
            </div>
            <div v-if="modalType === 'overpaymentModal'">
                <receiptInfo :dataDict="dataDict" :contractNo="contractNo"/>
            </div>
            <div slot="footer">
                <Button>
                    <Icon type="md-print"/>
                </Button>
                <Button type="primary" @click="closeModal">关闭</Button>
            </div>
        </Modal>
        <Spin size="large" fix v-if="spinShow"></Spin>
    </div>
</template>
<script>
    import {mapGetters} from "vuex";
    import {approveOverpayment, submitDetails, submitOverpayment} from "_p/basic/api/contract/contract-close/index";
    import expenseInfo from "./components/modal/expenseInfo";
    import receiptInfo from "./components/modal/receiptInfo";
    import {getDictDataList} from "_p/basic/assets/contract/js/utils";
    import contractDifference from "./components/formItem/contractDifference";
    import contractOverpayment from "./components/formItem/contractOverpayment";
    import contractInfo from "_p/basic/pages/contract/common/contract-info/contract-info";
    import approveHistory from "_p/basic/pages/contract/common/approve-history/approve-history";
    import contractCurrentUser from "_p/basic/pages/contract/common/current-user/contractCurrentUser";
    import moment from "moment";

    export default {
        name: "ContractCloseDetial",
        components: {
            approveHistory,
            contractInfo,
            contractOverpayment,
            contractDifference,
            contractCurrentUser,
            expenseInfo,
            receiptInfo
        },
        props: {},
        data() {
            return {
                processStatus: null,
                approveType: null,
                openPanel: ["2", "3", "4", "5"],
                type: null,
                Obj: {},
                isApprove: null,
                readOnly: false,
                contractNo: null,
                caseNo: null,
                id: null,
                pageInfo: {},
                loading: true,
                spinShow: false,
                contractForm: {},
                contractFormValidate: {},
                // 数据字典
                dicttArr: [
                    "processMode",
                    "bankCode",
                    "feeType",
                    "receiptSourceType",
                    "writeTarget"
                ],
                // selct 数据字典
                dataDict: {},
                // 弹窗状态
                modalType: "",
                modalTitle: "",
                modal: false,
            };
        },
        created() {
            this.getParams();
        },
        mounted() {
            this.init();
        },
        methods: {
            init() {
                // 数据字典
                this.getDictDataList();
            },
            // 获取初始数据
            getParams() {
                const params = this.afs.getPageParams(this);
                const {contractNo, caseNo, processStatus, readOnly} = params;
                this.processStatus = processStatus;
                this.pageInfo = params;
                this.type = params.type;
                this.readOnly = readOnly === "read" ? true : false;
                this.contractNo = contractNo;
                this.caseNo = caseNo;
                this.id = params.id;
                this.Obj = {
                    type: 'closeContract',
                    process: readOnly,
                    roleType: false,
                    readOnly: this.readOnly,
                    processStatus: processStatus,
                    caseNo: params.caseNo,
                    contractNo: params.contractNo
                };
                this.isApprove =
                    !!processStatus &&
                    (processStatus === "repairedUnderReview" ||
                        processStatus === "underReview");
            },
            getDictDataList() {
                getDictDataList(this.dicttArr).then(res => {
                    this.dataDict = res;
                });
            },
            save() {
                if (this.$refs.business) {
                    this.showSpin();
                    this.saveDataFn();
                }
            },
            submit() {
                this.$refs.business.formValidate("submitDataFn");
            },
            // 审核等按钮
            approve(name) {
                if (!name) return;
                let t, val;
                switch (name) {
                    case "pass":
                        t = "审核通过";
                        val = "suggestCheckF";
                        break;
                    case "return":
                        t = "退件";
                        val = "sendBack";
                        break;
                    case "reject":
                        t = "拒绝";
                        val = "suggestRejectF";
                        break;
                }
                this.approveType = name;
                this.approvalOperating = val;
                if (name === 'pass') {
                    // 验证 business 组件数据 给下一个方法
                    this.$refs.business.formValidate("childBusinessForm");
                } else {
                    this.childBusinessForm(this.$refs.business.formData);
                }

            },
            // 保存 请求
            saveDataFn() {
                let d = this.formatData(this.$refs.business.formData);
                d.custName = this.$refs.contractInfo.formData.basicCustBaseInfo.custName;
                d.contactNo = this.$refs.contractInfo.formData.basicCustBaseInfo.phone;
                submitDetails(d)
                    .then(res => {
                        this.closeSpin();
                        if (res.code === "0000") {
                            this.$Message.success("保存成功!");
                            this.pageInfo = res.data;
                        }
                    })
                    .catch(e => {
                        this.closeSpin();
                    });
            },
            //提交 请求
            submitDataFn(data) {
                let val = "suggestCheckF";
                data.approvalOperating = val;
                this.$Modal.confirm({
                    title: "确认提交？",
                    onOk: () => {
                        this.submitOverpayment(data);
                    }
                });
            },
            submitOverpayment(data) {
                if (!data) return;
                let d = this.formatData(data);
                submitOverpayment(d)
                    .then(res => {
                        this.closeSpin();
                        if (res.code === "0000") {
                            this.$Message.success("提交成功!");
                            this.back();
                        }
                    })
                    .catch(e => {
                        this.closeSpin();
                    });
            },
            // 审批 请求
            // Business 组件数据
            childBusinessForm(data) {
                if (!data) return;
                let bussiness = this.formatData(data);
                // 验证 contractCurrentUser 组件数据 传给下个方法
                this.$refs.contractCurrentUser.formValidate(
                    "childUserForm",
                    bussiness,
                    this.approveType
                );
            },
            // contractCurrentUser 组件数据
            childUserForm(data) {
                if (!data) return;
                let d = Object.assign({}, data, {
                    approvalOperating: this.approvalOperating
                });
                let t;
                switch (d.approvalOperating) {
                    case "suggestCheckF":
                        t = "通过";
                        break;
                    case "sendBack":
                        t = "退件";
                        break;
                    case "suggestRejectF":
                        t = "拒绝";
                        break;
                }
                this.$Modal.confirm({
                    title: `确认${t}？`,
                    onOk: () => {
                        this.saveProposalApplicationInfo(d)
                    }
                });
            },
            saveProposalApplicationInfo(d) {
                approveOverpayment(d)
                    .then(res => {
                        this.closeSpin();
                        if (res.code === "0000") {
                            this.$Message.success(res.msg);
                            this.back();
                        }
                    })
                    .catch(e => {
                        this.closeSpin();
                    });
            },
            // 格式化
            formatData(data) {
                if (!data) return {};
                let d = Object.assign({}, this.pageInfo, data);
                if (this.isSP) {
                    d.department = d.channelFullName;
                } else {
                    d.department = this.userInfo.userRealName;
                }
                if (d.createTime instanceof Date)
                    d.createTime = moment(d.createTime).format(
                        "YYYY-MM-DD HH:mm:ss"
                    );
                if (d.endDate instanceof Date)
                    d.endDate = moment(d.endDate).format("YYYY-MM-DD HH:mm:ss");
                return d;
            },
            // 弹窗
            showModal(type) {
                this.modalType = type;
                if (type === "differenceModal") {
                    this.modalTitle = "差额明细";
                } else if (type === "overpaymentModal") {
                    this.modalTitle = "溢缴款明细";
                }
                this.modal = true;
            },
            closeModal() {
                this.modal = false;
                this.modalTitle = null;
                this.modalType = null;
            },
            showSpin() {
                this.spinShow = true;
            },
            closeSpin() {
                this.spinShow = false;
            },
            back() {
                //关闭页面
                this.afs.closeTab(this);
            }
        },
        computed: {
            ...mapGetters({
                userInfo: "userInfo"
            })
        }
    };
</script>
<style lang="less">
    @import url("../../../styles/common/form.less");
</style>
